local renxie = fk.CreateSkill{
  name = "xiaobai__renxie",
}

Fk:loadTranslationTable{
  ["xiaobai__renxie"] = "衽挟",
  [":xiaobai__renxie"] = "你使用伤害牌指定目标后，可令目标角色依次交给你至少一张牌，则将此牌的伤害值改为你手牌中缺少的类别数。"..
    "目标角色因此受到伤害后，其可以令你获得其一张牌，然后回复1点体力。",
  ["#renxie_get"] = "衽挟：你可令目标角色依次交给你至少一张牌，然后将此牌的伤害值改为你手牌中缺少的类别数。",
  ["#renxie_give"] = "衽挟：你可以令 %dest 获得你一张手牌，然后你回复1点体力。",

  ["$xiaobai__renxie1"] = "蟊贼听真：一刻不见老母，某便再剖一人。",
  ["$xiaobai__renxie2"] = "某家新丰鲍文才，好生思量，莫要自误。",
}

renxie:addEffect(fk.TargetSpecified, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return (target == player and player:hasSkill(self) and (data.firstTarget and #data:getAllTargets() > 0 and data.card.is_damage_card) 
      or player:getMark("#renxie_damage") ~= 0)
  end,
  on_cost = function (self, event, target, player, data)
    if data.firstTarget then
      return player.room:askToSkillInvoke(player, {
        skill_name = renxie.name,
        prompt = "#renxie_get"})
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if data.firstTarget then
      local card = data.card
    card.skillName = renxie.name
    table.forEach(data:getAllTargets(), function (element, index, array)
      if element:isNude() then
        return
      end
      local cards = player.room:askToCards(element, {
        min_num = 1,
        max_num = 999,
        include_equip = true,
        skill_name = renxie.name,
        cancelable = false,
        pattern = "."})
      player.room:obtainCard(player, cards, false, fk.ReasonGive, element, renxie.name)
    end)

    local num  = {false, false, false}
    local hurt = 2
    table.forEach(player:getCardIds("h"),function (element, index, array)
      local card = Fk:getCardById(element)  
      if num[card.type] == false then
        hurt = hurt - 1
        num[card.type] = true
      end
    end)
    data.additionalDamage = (data.additionalDamage or 0) + hurt
    player.room:setPlayerMark(player, "#renxie_damage", hurt)
    local use = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, false)
    if not use then return end
    use:addCleaner(function ()
      player.room:setPlayerMark(player, "#renxie_damage", 0)
    end)
    data.card.extra_data = data.card.extra_data or {}
    data.card.extra_data.xiaobai__renxie = true
    else
      data.additionalDamage = (data.additionalDamage or 0) + player:getMark("#renxie_damage")
    end
  end,
})

renxie:addEffect(fk.Damaged, {
  can_trigger = function (self, event, target, player, data)
    return data.from and data.from == player and data.card and data.card.extra_data and data.card.extra_data.xiaobai__renxie and
        target.hp < target.maxHp and not target:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(target, {
      skill_name = renxie.name,
      prompt = "#renxie_give::"..player.id})
  end,
  on_use = function (self, event, target, player, data)
    local card = player.room:askToChooseCard(player, {
      target = target,
      flag = "he",
      skill_name = renxie.name})
    if not card then return end
    local cost_data = {}
    cost_data.card = card
    event:setCostData(self, cost_data)
    player.room:obtainCard(player, cost_data.card, false, fk.ReasonGive, player, renxie.name)
    if target.dead then return end
    player.room:recover{
      who = target,
      num = 1,
      recoverBy = player,
      skillName = renxie.name,
    }
  end
})

return renxie